package com.zzz.application;

import android.content.ContentProvider;
import android.content.Context;
import android.os.Build.VERSION_CODES;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.app.AppComponentFactory;
import com.zzz.mylibrary.util.LogZ;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Created by zhaojh on date 2021/9/15
 */
public class MyAppComponentFactory extends AppComponentFactory {
    
    @RequiresApi(api = VERSION_CODES.P)
    @NonNull
    @Override
    public ContentProvider instantiateProviderCompat(@NonNull ClassLoader cl, @NonNull String className)
        throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        
        //经过log可看出，有N个Provider，就会调用N次Provider
        LogZ.e("pass instantiateProviderCompat" + className);
        
        //这里把指定的Provider拦截下来，等点击同意协议后调用
        ContentProvider interupt = super.instantiateProviderCompat(cl, className);
        map.put(className, interupt);
        return interupt;
    }
    
    public static HashMap<String, ContentProvider> map = new HashMap<>(8);
    
    public static void lazyInitProvider(Context context) {
        if (!map.isEmpty()) {
            Iterator iterator=map.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry entry = (Entry) iterator.next();
                ContentProvider interupt = (ContentProvider) entry.getValue();
            }
        }
    }
}
